home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Dialogs / AGSupprt.cpp next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  14.3 KB  |  614 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AGSupprt.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Yan Arrouye
  7.  
  8.     Copyright:    © 1995-1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      7/8/96    eeh        undo task 10008 (AppleGuide buttons)
  13.          <7>     5/16/96    Yan        Style check
  14.          <6>     5/09/96    Yan        Fixed leak
  15.          <5>     5/08/96    Yan        fixed momory leaks
  16.          <4>     4/25/96    Yan        Use CopyPascalString instead of BlockMoveData
  17.          <3>     2/20/96    tb        Placed #if qDebug around TraceMonitor support.
  18.          <2>    10/26/95    Yan        • Added support for CodeWarrior. Requires that you rename the
  19.                                     AppleGuideGlue.xcoff file to AppleguideGlueLib to link.                                    
  20.                  8/31/95    Yan        Moved to ShareWare
  21.  
  22.     To Do:
  23. */
  24.  
  25.  
  26.  
  27. #ifndef _AGSUPPORT_
  28. #include "AGSupprt.h"
  29. #endif
  30.  
  31. #ifndef _DLGDEFS_
  32. #include "DdgDefs.h"
  33. #endif
  34.  
  35. #ifndef _TEMPOBJ_
  36. #include "TempObj.h"
  37. #endif
  38.  
  39. #ifndef _USERSRCM_
  40. #include <UseRsrcM.h>
  41. #endif
  42.  
  43. #ifndef __CONTROLS__
  44. #include "Controls.h"
  45. #endif
  46.  
  47. #ifndef __ICONS__
  48. #include "Icons.h"
  49. #endif
  50.  
  51.  
  52. //#include "Utilities.h"
  53.  
  54. #if qDebug
  55. #include "TraceMonitor.h"
  56. #define kModuleChannel    'GUID'
  57. #endif
  58.  
  59. #include <AppleGuide.h>
  60. #include <Balloons.h>
  61. #include <Gestalt.h>
  62. #include <Folders.h>
  63. #include <Files.h>
  64. #include <Menus.h>
  65. #include <TextUtils.h>
  66.  
  67.  
  68.  
  69. #if GENERATING68K
  70. #pragma segment AppleGuide
  71. #endif
  72.  
  73. /*******************************************************************************
  74. **    Globals
  75. *******************************************************************************/
  76. #define AG_BETA_RECEIVED
  77.  
  78.  
  79. #if PRAGMA_ALIGN_SUPPORTED
  80. #pragma options align=mac68k
  81. #endif
  82.  
  83. // Runtime structure
  84. typedef struct
  85. {
  86.     FSSpec        spec;
  87.     AGRefNum    ref;
  88. } InstalledGuideRec;
  89.  
  90. typedef struct
  91. {
  92.     short                count;
  93.     InstalledGuideRec    guide[1];
  94. } InstalledGuideList, **InstalledGuideListHdl;
  95.  
  96.  
  97. // Resources
  98. typedef struct {
  99.     Str32    name;
  100.     char    filler;
  101.     Str255    itemTitle;
  102. } GuideEntry;
  103.  
  104.  
  105. typedef struct {
  106.     short        count;
  107.     GuideEntry    guide[1];
  108. } GuideList, **GuideListHdl;
  109.  
  110.  
  111.  
  112. #if PRAGMA_ALIGN_SUPPORTED
  113. #pragma options align=reset
  114. #endif
  115.  
  116.  
  117.  
  118. InstalledGuideListHdl    gInstalledGuidesList        = NULL;
  119. short                    gInstalledGuidesListSize    = 0;
  120. short                    gFirstAppleGuideItem        = 0;
  121. ODBoolean                gAGInstallAttempted            = kODFalse;
  122. ODBoolean                gAGInstallSucceeded            = kODFalse;
  123. FSSpec                    gFileSpec                    = { 0, 0, "\p" };
  124. unsigned long            gRefNum                        = 0;
  125.  
  126. /*******************************************************************************
  127. **    Boolean IsAppleGuidePresent
  128. *******************************************************************************/
  129.  
  130. Boolean IsAppleGuidePresent()
  131. {
  132.     long    result = 0;
  133.     OSErr    err;
  134.     
  135.     err = Gestalt(gestaltHelpMgrAttr, &result);
  136.     
  137.     return ( err == noErr && (result & (1 << gestaltAppleGuidePresent)) != 0 );
  138. }
  139.  
  140.  
  141.  
  142. /*******************************************************************************
  143. **    Boolean IsAppleGuideInstalled
  144. *******************************************************************************/
  145.  
  146. Boolean IsAppleGuideInstalled()
  147. {
  148.     
  149.     return IsAppleGuidePresent() && gInstalledGuidesListSize != 0;
  150. }
  151.  
  152.  
  153.  
  154. /*******************************************************************************
  155. **    void InstallGuide
  156. *******************************************************************************/
  157.  
  158. static void FindAppleGuideFolder( short *vol, long *dir );
  159. static void FindAppleGuideFolder( short *vol, long *dir )
  160. {
  161. //    Str255    folderName;
  162.     
  163.     CInfoPBRec        pb;
  164.     memset (&pb,0,sizeof(pb));
  165.  
  166.         //First find System Folder
  167.     OSErr err = FindFolder(kOnSystemDisk, kExtensionFolderType,
  168.             kDontCreateFolder, &pb.dirInfo.ioVRefNum, &pb.dirInfo.ioDrDirID );
  169.  
  170.     pb.dirInfo.ioNamePtr = (StringPtr)"\pGlobal Guide Files";
  171.     pb.dirInfo.ioFDirIndex = 0;
  172.     err = PBGetCatInfoSync( &pb );
  173.  
  174.     *vol = pb.dirInfo.ioVRefNum;
  175.     *dir = pb.dirInfo.ioDrDirID;
  176. }
  177.  
  178. void InstallAppleGuide()
  179. {
  180.     if ( !gAGInstallAttempted && IsAppleGuidePresent() )
  181.     {
  182.         gAGInstallAttempted = kODTrue;        // only try once -- so we aren't always failing
  183.  
  184.         short vol;
  185.         long dir;
  186.         FindAppleGuideFolder( &vol, &dir );
  187.  
  188.         OSErr err = FSMakeFSSpec( vol, dir, "\pOpenDoc Guide", &gFileSpec );
  189.  
  190.  
  191. #if 0 /* AG_BETA_RECEIVED */
  192.         // Look in the Extensions folder the Macintosh guide and add it to the help menu as needed
  193.         OSErr    err;
  194.         short    vRefNum;
  195.         long    dirID;
  196.         
  197.         err = FindFolder(kOnSystemDisk, kExtensionFolderType, kDontCreateFolder,
  198.                 &vRefNum, &dirID);
  199.  
  200.         if ( err == noErr )
  201.         {
  202.             // If there is a guide specification, use it
  203.             GuideListHdl guideList = (GuideListHdl)Get1Resource(kAppleGuidesList, 128);
  204.             
  205.             if ( guideList != NULL )
  206.             {
  207.                 // There is a list of guides
  208.                 TempODHandleLock lock((Handle)guideList);
  209.                 MenuHandle    helpMenu = NULL;
  210.                 
  211.                 short count = (**guideList).count;
  212.                 gInstalledGuidesList = (InstalledGuideListHdl)NewHandle(
  213.                         sizeof(InstalledGuideRec) * count );
  214.                 
  215.                 HMGetHelpMenuHandle(&helpMenu);
  216.                 if ( helpMenu != NULL )
  217.                 {
  218.                     gFirstAppleGuideItem = CountMItems(helpMenu) + 1;
  219.                 }
  220.                 #if qDebug
  221.                 else
  222.                 {
  223.                     Trace(kModuleChannel, kFatalMessageLevel, "•• No help menu\n");
  224.                 }
  225.                 #endif
  226.                 
  227.                 // Create the runtime list
  228.                 if ( gInstalledGuidesList != NULL )
  229.                 {
  230.                     short    i;
  231.                     FSSpec    spec;
  232.                     
  233.                     for ( i = 0; i < count; i++ )
  234.                     {
  235.                         err = FSMakeFSSpec(vRefNum, dirID,
  236.                         (**guideList).guide[i].name, &spec);
  237.                         
  238.                         if ( err == noErr )
  239.                         {
  240.                             gAGInstallSucceeded = kODTrue;        // we've got at least one.
  241.  
  242.                             // Guide found: stuff spec in list
  243.                             BlockMoveData(&spec,
  244.                                 &(**gInstalledGuidesList).
  245.                                 guide[gInstalledGuidesListSize].spec,
  246.                                 sizeof(spec));
  247.                             (**gInstalledGuidesList).guide[gInstalledGuidesListSize]
  248.                                 .ref = 0;
  249.                             
  250.                             #if qDebug
  251.                             Trace(kModuleChannel, kFatalMessageLevel, "• Adding guide '%*.*s', menu item '%*.*s'\n", (**guideList).guide[i].name[0], (**guideList).guide[i].name[0], (**guideList).guide[i].name + 1, (**guideList).guide[i].itemTitle[0], (**guideList).guide[i].itemTitle[0], (**guideList).guide[i].itemTitle + 1);
  252.                             #endif
  253.                             
  254.                             // Install guide in menu
  255.                             if ( helpMenu != NULL )
  256.                             {
  257.                                 AppendMenu(helpMenu,
  258.                                         (**guideList).guide[i].itemTitle);
  259.                             }
  260.                             
  261.                             // Increment installed guides list list index
  262.                             gInstalledGuidesListSize++;
  263.                         }
  264.                         #if qDebug
  265.                         else
  266.                         {
  267.                             Trace(kModuleChannel, kFatalMessageLevel, "•• Error %d creating spec for guide '%*.*s'\n", err, (**guideList).guide[i].name[0], (**guideList).guide[i].name[0], (**guideList).guide[i].name + 1);
  268.                         }
  269.                         #endif
  270.                     }
  271.                     
  272.                     // Adjust list size
  273.                     SetHandleSize((Handle)gInstalledGuidesList,
  274.                             gInstalledGuidesListSize * sizeof(InstalledGuideRec) );
  275.                 }
  276.                 #if qDebug
  277.                 else
  278.                 {
  279.                     Trace(kModuleChannel, kFatalMessageLevel, "•• Can't allocate installed guides list\n", err);
  280.                 }
  281.                 #endif
  282.             }
  283.             #if qDebug
  284.             else
  285.             {
  286.                 Trace(kModuleChannel, kFatalMessageLevel, "•• No 'AGd#' id 128 resource\n", err);
  287.             }
  288.             #endif
  289.         }
  290.         #if qDebug
  291.         else
  292.         {
  293.             Trace(kModuleChannel, kFatalMessageLevel, "•• Error %d finding extensions folder\n", err);
  294.         }
  295.         #endif
  296. #endif    /* AG_BETA_RECEIVED */
  297.     }
  298.     #if qDebug
  299.     else
  300.     {
  301.         Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
  302.     }
  303.     #endif
  304. }
  305.  
  306.  
  307.  
  308. /*******************************************************************************
  309. **    void CloseAppleGuide
  310. *******************************************************************************/
  311.  
  312. void CloseAppleGuide()
  313. {
  314. #ifdef AG_BETA_RECEIVED
  315.     if ( IsAppleGuidePresent() == true )
  316.     {
  317.         short i;
  318.         
  319.         for ( i = 0; i < gInstalledGuidesListSize; i++ )
  320.         {
  321.             AGRefNum ref = (**gInstalledGuidesList).guide[i].ref;
  322.             
  323.             if ( ref != 0 && AGIsDatabaseOpen(ref) == true )
  324.             {
  325.                 // Guide already open
  326.                 AGClose(&ref);
  327.                 (**gInstalledGuidesList).guide[i].ref = 0;
  328.             }
  329.         }
  330.         
  331.         if ( gInstalledGuidesList ) 
  332.         {
  333.             DisposeHandle((Handle)gInstalledGuidesList);
  334.             gInstalledGuidesList = NULL;
  335.         }
  336.         AGQuit();
  337.     }
  338.     #if qDebug
  339.     else
  340.     {
  341.         Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
  342.     }
  343.     #endif
  344. #endif    /* AG_BETA_RECEIVED */
  345. }
  346.  
  347.  
  348.  
  349.  
  350. /*******************************************************************************
  351. **    Boolean OpenThisGuide
  352. *******************************************************************************/
  353.  
  354. static Boolean OpenThisGuide(FSSpec* guide, AGRefNum* refNum,
  355.         StringPtr keyword = NULL)
  356. {
  357.     Boolean guideOpened = false;
  358.     
  359.     if ( IsAppleGuidePresent() )
  360.     {
  361.         if ( AGIsDatabaseOpen(*refNum) == true )
  362.         {
  363.             // Guide already open
  364.             AGClose(refNum);
  365.             *refNum = 0;
  366.         }
  367.         
  368.         #if qDebug
  369.         Trace(kModuleChannel, kFatalMessageLevel,
  370.                 "•• Looking for keyword '%*.*s'\n", keyword[0], keyword[0],
  371.                 keyword+1);
  372.         #endif
  373.         
  374.         // Open AppleGuide
  375.         if ( guide->name[0] != 0 )
  376.         {
  377.             AGErr        err;
  378.                         
  379.             // Open AppleGuide
  380.             if ( keyword != NULL && keyword[0] != 0 )
  381.             {
  382.                 // Open using the specified keyword
  383.                 err = AGOpenWithSearch( guide, 0, NULL, keyword, refNum);
  384.             }
  385.             else
  386.             {
  387.                 // Open root window
  388.                 err = AGOpenWithView( guide, 0, NULL, kAGViewFullHowdy, refNum);
  389.             }
  390.             
  391.             if ( err == noErr )
  392.             {
  393.                 guideOpened = true;
  394.             }
  395.             #if qDebug
  396.             else
  397.             {
  398.                 Trace(kModuleChannel, kFatalMessageLevel, "••• AppleGuide opening error %d\n", err);
  399.             }
  400.             #endif
  401.         }
  402.         #if qDebug
  403.         else
  404.         {
  405.             Trace(kModuleChannel, kFatalMessageLevel, "•• No guide spec!\n");
  406.         }
  407.         #endif
  408.     }
  409.     #if qDebug
  410.     else
  411.     {
  412.         Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
  413.     }
  414.     #endif
  415.  
  416.     return guideOpened;
  417. }
  418.  
  419.  
  420. /*******************************************************************************
  421. **    Boolean OpenAppleGuide
  422. *******************************************************************************/
  423.  
  424. Boolean OpenAppleGuide(StringPtr keyword)
  425. {
  426.     Boolean opened = false;
  427. #ifdef AG_BETA_RECEIVED    
  428.     if ( (gInstalledGuidesListSize > 0) || (gFileSpec.name[0] != 0) )
  429.     {
  430.         TempODHandleLock    list((Handle)gInstalledGuidesList);
  431.         
  432.         
  433. //        opened = OpenThisGuide( kAGDefault, &gRefNum, keyword );
  434.         opened = OpenThisGuide( &gFileSpec, &gRefNum, keyword );
  435.  
  436. //        opened =  OpenThisGuide( &(**gInstalledGuidesList).guide[0].spec,
  437. //                &(**gInstalledGuidesList).guide[0].ref, keyword);
  438.     }
  439. #endif    /* AG_BETA_RECEIVED */
  440.     return opened;
  441. }
  442.  
  443. /*******************************************************************************
  444. **    Boolean OpenAppleGuideMenuItem
  445. *******************************************************************************/
  446.  
  447. #if 0
  448. Boolean OpenAppleGuideMenuItem(short item)
  449. {
  450.     Boolean opened = false;
  451.     
  452.     if ( IsAppleGuidePresent() )
  453.     {
  454.         TempODHandleLock    list((Handle)gInstalledGuidesList);
  455.  
  456.         opened = OpenThisGuide((**gInstalledGuidesList).guide[item - gFirstAppleGuideItem].spec, (**gInstalledGuidesList).guide[item - gFirstAppleGuideItem].ref);
  457.     }
  458.     
  459.     return opened;
  460. }
  461. #endif
  462.  
  463. /*******************************************************************************
  464. **    InitAppleGuideSupport
  465. *******************************************************************************/
  466. //short gAGButtonItem = -1;
  467. ControlDefUPP gCtrlDefUPP = NULL;
  468. Handle ctrlDefStub = NULL;
  469. Handle gIconSuite = NULL;
  470.  
  471. const short kControlInactive = 255;
  472. const short kControlActive = 0;
  473. const short kCDEFAddrOffset = 8;
  474.  
  475. pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
  476.         short message, long param );
  477.  
  478. void InitAppleGuideSupport()
  479. {
  480.     CUsingLibraryResources r;
  481.     InstallAppleGuide();        // returns immediately if installed
  482.     
  483.     // Must fix up the CDEF resource whether the install succeeded
  484.     // or not; else we'll jsr to 0.
  485.     if ( !gCtrlDefUPP )
  486.     {
  487.         gCtrlDefUPP = NewControlDefProc( AGBUTTONCDEF );
  488.         ctrlDefStub = Get1Resource( 'CDEF', kAGButtonCDEFId);
  489.         WASSERT( ctrlDefStub != NULL );
  490.         (*(ControlDefUPP*)&((*ctrlDefStub)[kCDEFAddrOffset])) = gCtrlDefUPP;
  491.     }
  492. }
  493.  
  494. /*******************************************************************************
  495. **    TakedownAppleGuideSupport
  496. *******************************************************************************/
  497.  
  498. void TakedownAppleGuideSupport()
  499. {
  500.     if ( gCtrlDefUPP )
  501.     {
  502.         DisposeRoutineDescriptor( gCtrlDefUPP );
  503.         gCtrlDefUPP = NULL;
  504.     }
  505.     if ( ctrlDefStub )
  506.     {
  507.         CUsingLibraryResources r;    // <eeh> find out if necessary
  508.         ReleaseResource( ctrlDefStub );
  509.         ctrlDefStub = NULL;
  510.     }
  511.     if ( gIconSuite )
  512.     {
  513.         DisposeIconSuite( gIconSuite, true );
  514.         gIconSuite = NULL;
  515.     }
  516.     CloseAppleGuide();
  517. }
  518.  
  519. /*******************************************************************************
  520. **    TakedownAppleGuideSupport
  521. *******************************************************************************/
  522.  
  523. void DialogSetUpAppleGuide( DialogPtr dlg, short item )
  524. {
  525. //    gAGButtonItem = item;
  526.     
  527.     short             itemType;
  528.     ControlHandle    cntrlHandle;
  529.     Rect scratchRect;
  530.     GetDialogItem(dlg, item, &itemType, (Handle*)&cntrlHandle,
  531.             &scratchRect );
  532.  
  533.     // <eeh> don't check this in!!!!
  534.     gAGInstallSucceeded  = true;
  535.  
  536.     HiliteControl( cntrlHandle,
  537.             gAGInstallSucceeded? kControlActive : kControlInactive);
  538.  
  539.     if ( gIconSuite == NULL )
  540.     {
  541.         CUsingLibraryResources r;
  542.         short iconsID = (*cntrlHandle)->contrlRfCon;
  543.         // ignore error; gIconSuite *should* remain null.
  544.         OSErr err = GetIconSuite( &gIconSuite, iconsID,
  545.             kSelectorAllSmallData );
  546.         if ( err != noErr )
  547.         {
  548.             gIconSuite = NULL;
  549.             WARN( "GetIconSuite returned null" );
  550.         }
  551.     }
  552. }
  553.  
  554. IconTransformType gCurrentTransform = kTransformNone;
  555.  
  556. void Plot( ControlHandle ctrlH, IconTransformType trans );
  557. void Plot( ControlHandle ctrlH, IconTransformType trans )
  558. {
  559.     if ( gIconSuite )
  560.     {
  561.         OSErr err = PlotIconSuite( &(*ctrlH)->contrlRect,
  562.                 kAlignNone, gCurrentTransform=trans,
  563.                 (Handle)gIconSuite );
  564.         WASSERT( err == noErr );
  565.     }
  566. }
  567.  
  568. pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
  569.         short message, long param )
  570. {
  571.     long result = 0;
  572.     switch ( message )
  573.     {
  574.         case drawCntl:
  575.             if ( (*ctrlH)->contrlVis /* == 255 visible */ )
  576.             {
  577.                 short activeCode = (*ctrlH)->contrlHilite;
  578.                 switch ( activeCode )
  579.                 {
  580.                     case kControlInactivePart:
  581.                         Plot( ctrlH, kTransformDisabled );
  582.                         break;
  583.                     case kControlButtonPart:
  584.                         Plot( ctrlH, kTransformSelected );
  585.                         break;
  586.                     default:
  587.                         Plot( ctrlH, kTransformNone );
  588.                 }
  589.             }
  590.             break;
  591.         case testCntl:
  592.             Point pt = *(Point*)¶m;
  593.             if ( PtInRect( pt, &(*ctrlH)->contrlRect ) )
  594.             {
  595.                 result = kControlButtonPart;
  596.                 if ( gCurrentTransform == kTransformNone )
  597.                     Plot( ctrlH, kTransformSelected );
  598.             }
  599.             break;
  600.  
  601.         // we could use these rather than the methods above to do ifam init
  602. //        case initCntl:
  603. //        case dispCntl:
  604. //            break;
  605.  
  606.         // case calcCRgns:    // not an issue for OpenDoc
  607.         case calcCntlRgn:
  608.             param &= 0x7fffffff;        // clear high bit (NIM 5-112)
  609.             RectRgn( (RgnHandle)param, &(*ctrlH)->contrlRect );
  610.             break;
  611.         
  612.     }
  613.     return result;
  614. }